Ext.define('CA.dictionary.DataPanel',{
    extend: 'Ext.Panel',
    xtype: 'CA.dictionary.DataPanel',

    uses: [
        'CA.dictionary.TreeData',
        'CA.dictionary.LineData'
    ],

    initComponent: function(){
        Ext.apply(this, {
            tbar: [
                {
                    text: 'Новая запись',
                    scope: this,
                    handler: this.createRecord
                },
                '-',
                {
                    text: 'Удалить',
                    iconCls: 'c-icon-cancel',
                    scope: this,
                    handler: this.removeRecord
                },
                '->',
                {
                    iconCls: 'c-icon-reload',
                    text: 'Обновить',
                    handler: this.doLoad,
                    scope: this
                }
            ]
        });

        this.on('afterrender', this.doLoad, this);
        this.callParent();
    },

    doLoad: function(){
        var el = this.getEl();

        el.mask('Загрузка...');

        Dictionary.load(this.dictionaryId, function(resp){
            if(this.dataPanel){
                this.dataPanel.destroy();
            }

            var type = resp.data.view_type == 'line'
                ? 'CA.dictionary.LineData'
                : 'CA.dictionary.TreeData';

            this.dataPanel = this.add({
                xtype: type,
                layout: 'fit',
                border: false,
                dictionary: resp
            });

            el.unmask();
        }, this);
    },

    createRecord: function(){
        this.dataPanel.createRecord();
    },

    removeRecord: function(){
        this.dataPanel.removeSelected();
    }
});
